Skip to content

fix(core): correctly discover value providers in discovery service - #17618

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
shariyerShazan:fix/discovery-usevalue-metadata
Aug 29, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
shariyerShazan:fix/discovery-usevalue-metadata

Conversation

@shariyerShazan

@shariyerShazan shariyerShazan commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #17617

What was wrong

DiscoveryService.getProviders({ metadataKey }) failed to return providers registered via useValue when the target class was decorated with a custom decorator from DiscoveryService.createDecorator(). Only useClass-style providers were discovered.

Why it happened

In DiscoverableMetaHostCollection.getMetaKeyByInstanceWrapper, the ternary condition was missing a negation that the accompanying code comment explicitly describes:

// - The condition `!wrapper.metatype` is needed because when we use `useValue`
// the value of `wrapper.metatype` will be `null`.
...
instanceWrapper.metatype || instanceWrapper.inject   // bug: missing `!`

For useValue providers, metatype is null and inject is undefined, so the condition evaluated falsy and never fell back to instance.constructor, leaving the lookup keyed on null.

What was changed

Added the missing negation:

!instanceWrapper.metatype || instanceWrapper.inject

This restores the fallback to instance.constructor for useValue providers while leaving the useClass/useFactory behavior unchanged.

How it was tested

  • Updated an existing unit test in discoverable-meta-host-collection.spec.ts that previously asserted the incorrect behavior — it now asserts the provider is correctly discovered.
  • Added a new regression test in discovery-service.spec.ts covering DiscoveryService.getProviders({ metadataKey }) with a useValue provider.
  • Ran the full discovery test suite locally (packages/core/test/discovery/) — all 45 tests pass.
  • Ran scoped linting on the changed files — 0 warnings, 0 errors.

Why the change is safe

The fix only affects the branch previously reached when metatype is falsy (i.e., useValue providers with no inject). Existing useClass and useFactory code paths are unaffected, since metatype/inject remain truthy for those cases and the same branch is taken as before.

Performance impact

The old condition read wrapper.instance (the getter the comment says to defer) for every useClass provider — the dominant case in most apps. The new condition only reads it for useValue and useFactory providers:

Provider kind Old condition reads .instance New condition reads .instance
class / useClass yes no
useFactory yes yes
useValue no yes

So this change reduces reads of that getter overall rather than adding to them — it doesn't work against the deferred-access optimization the comment describes.

(Credit to @johnnyhuirilef for working this out and confirming independently.)

Providers registered via useValue with a custom discovery decorator
were not being discovered by DiscoveryService.getProviders because a
ternary condition in DiscoverableMetaHostCollection was missing a
negation operator.

Fixes nestjs#17617
@johnnyhuirilef

Copy link
Copy Markdown
Contributor

I ran into the same issue and arrived at the same change, so I'll leave this one to you — yours covers more ground with the DiscoveryService test. Some context I picked up along the way, in case it's useful if anyone asks:

The comment and the typo landed together. Both come from 7e3da2253 (2023-07-24), and the note has said !wrapper.metatype from the first day while the code never had the negation. git log -S '!wrapper.metatype' returns only that one commit, so the note was never edited — it isn't documentation that drifted, it's the original intent.

The spec you changed is a characterization test. It was added in 5ea3988a8 (2025-12-13), 2.5 years later, in a coverage pass that touched no source files. Inverting it turns it from pinning the bug into guarding against it, which is what you did.

On the performance note, which is what I'd expect to come up: it says wrapper.instance should be deferred, which makes negating the condition sound like it would make that worse. It goes the other way —

provider kind old condition reads .instance new
class / useClass yes no
useFactory yes yes
useValue no yes

The old condition was truthy for every class provider, which is the dominant case in an app; the new one is truthy only for useValue and factories. The change reduces reads of that getter rather than adding them.

Happy to help verify if that's useful.

@shariyerShazan

Copy link
Copy Markdown
Contributor Author

Thanks @johnnyhuirilef — really appreciate the extra context, especially the git archaeology on the comment/typo landing together in the same commit. That performance breakdown is a great point to have on record; happy for maintainers to reference it during review if the deferred-instance-access concern comes up.

@kamilmysliwiec

Copy link
Copy Markdown
Member

lgtm

@kamilmysliwiec
kamilmysliwiec merged commit 9b71cad into nestjs:master Aug 29, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DiscoveryService.getProviders({ metadataKey }) fails to discover providers registered via useValue

3 participants